"""Riemann solver for the Burger's equation"""

def burgers_flux(q):
    return 0.5 * q*q

class BurgersExact(object):
    """Riemann solver for the Burger's equation.

    The solution for the inviscid Burger's equation is descrbed in
    section (2.4.2) of Toro.

    """
    def __init__(self):
        self.nwaves = 1
    
    def godunov_solution(self, ql, qr):
        """Return the Godunov solution for the Burger's equation.

        The solution for the Burger's equation is given in equation
        (2.112) in Toro.
        
        """
        ul, ur = ql[0], qr[0]

        if self._is_shock(ql, qr):
            S = 0.5 * (ul + ur)
            if S >= 0:
                return ql
            else:
                return qr
        else:
            if ul >= 0:
                return ql
            elif ur < 0:
                return qr
            else:
                return 0.0

    def compute_waf_states(self, ql, qr, U, S):
        """Compute the intermediate states for the WAF method.
        """
        ul = ql[0]; ur = qr[0]

        U[:, 0] = ul
        U[:, 1] = ur

        if self._is_shock(ql, qr):
            S[0] = 0.5 * (ul + ur)

        else:
            if ul >= 0:
                S[0] = ur
            elif ur < 0:
                S[0] = ul
            else:
                S[0] = 0.5 * (ul + ur)
                U[:, 1] = 0.0

    def _is_shock(self, ql, qr):
        """Return True if the initial data results in a shock"""
        return ql[0] > qr[0]
